﻿using System;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using VideoSeller.Business.Models;
using VideoSeller.Core.Configuration;

namespace VideoSeller.Business.Extensions
{
	public static class HtmlExtension
	{
		public static MvcHtmlString Paging(this HtmlHelper htmlHelper, int pageNumber, int pageCount, Func<int, string> pageUrl)
		{
			var pageStart = ((pageNumber - 1) / 6) * 6 + 1;
			var pageEnd = pageStart + 6;

			if (pageEnd > pageCount)
			{
				pageEnd = pageCount;
			}

			var sb = new StringBuilder();

			if (pageNumber > 1 && pageNumber <= pageCount)
			{
				sb.AppendLine("<li><a href=" + pageUrl(pageNumber - 1) + ">←</a></li>");
			}

			for (var i = pageStart; i <= pageEnd; i++)
			{
				if (i == pageNumber)
				{
					sb.AppendLine("<li class=" + "active" + "><a href=" + pageUrl(i) + ">" + i + "</a></li>");
				}
				else
				{
					sb.AppendLine("<li><a href=" + pageUrl(i) + ">" + i + "</a></li>");
				}
			}

			if (pageNumber < pageCount)
			{
				sb.AppendLine("<li><a href=" + pageUrl(pageNumber + 1) + ">→</a></li>");
			}

			return MvcHtmlString.Create(sb.ToString());
		}

		public static MvcHtmlString RenderBlock(this HtmlHelper helper, string name)
		{
			var contentModel = IoC.Resolve<ContentModel>();
			var model = contentModel.GetBlock(name);

			return model == null ? MvcHtmlString.Empty : helper.Partial("~/Views/Content/_Block.cshtml", model);
		}
	}
}